// 世界boss
exports.get = function(player, req, resp, onHandled) {
    resp.nolog = 1;
    do{
        var user = player.user;
        
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
        
        var today = new Date();

        var todayStartTime = +(new Date(today.getFullYear(), today.getMonth(), today.getDate()))/1000;
        var resetTime = todayStartTime + gConfGlobal.WorldBossResetHour*3600 + 10;
        var startTime = todayStartTime + gConfGlobal.WorldBossStartHour1*3600 - 10;

        // 时间限制(重置到第一次开始期间不能发请求)
        if( nowTime >= resetTime && nowTime <= startTime ) {
            resp.code = 1; resp.desc = 'wrong time to get'; break;
        }
        
    }while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    req.args.join = user.world_boss.join;
    req.args.today_id = user.world_boss.today_id;
    req.args.got_reward = user.world_boss.got_reward;

    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            if( resp.data.bossid && resp.data.bossid != user.world_boss.join ||
                resp.data.today_id != user.world_boss.today_id ) {

                // 重置boss
                user.world_boss.join = resp.data.bossid;
                user.world_boss.today_id = resp.data.today_id;
                user.world_boss.got_reward = 0;
                user.world_boss.time = 0;
                user.world_boss.inspire = 0;
                player.markDirty('world_boss');

                resp.data.reset = 1;
            }
        }

        onHandled();
    });
}

exports.rank = function(player, req, resp, onHandled) {
    do{
        var user = player.user;
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
    }while(false);
        
    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, onHandled);
}

exports.get_kill_reward = function(player, req, resp, onHandled) {
    do{
        var user = player.user;
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
    }while(false);
        
    if( resp.code == 1 ) {
        onHandled();
        return;
    }
        
    requestWorld(req, resp, function() {    
        if( resp.code == 0 ) {
            var awards = []; 
            var worldBossRewardConf = gConfWorldBossReward[getThisWeek()]['kill'];
            if( worldBossRewardConf ) { 
                awards.push(worldBossRewardConf.Award1);
                awards.push(worldBossRewardConf.Award2);
            }

            resp.data.awards = player.addAwards(awards);
        }

        onHandled();
    });
}

exports.get_reward = function(player, req, resp, onHandled) {
    do {
        var user = player.user;
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
    }while(false);
        
    if( resp.code == 1 ) {
        onHandled();
        return;
    }
        
    req.args.got_reward = user.world_boss.got_reward;
    requestWorld(req, resp, function() {
        if( resp.code == 0 ) {
            user.world_boss.got_reward = resp.data.bossid;
            player.markDirty('world_boss.got_reward');

            resp.data.awards = player.addAwards(resp.data.awards);
        }

        onHandled();
    });
}

exports.fight = function(player, req, resp, onHandled) {
    var user = player.user;
    do {
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
            
        if( (common.getTime() - user.world_boss.time) < (gConfGlobal.WorldBossCoolTime-10) ) {
            resp.code = 1; resp.desc = 'in cool time'; break;
        }

        var nowTime = common.getTime();
        var today = new Date();

        var todayStartTime = +(new Date(today.getFullYear(), today.getMonth(), today.getDate()))/1000;
        var resetTime = todayStartTime + gConfGlobal.WorldBossResetHour*3600 - 10;
        var startTime = todayStartTime + gConfGlobal.WorldBossStartHour1*3600 - 10;

        // 时间限制(重置到开始期间不能发请求)
        if( nowTime >= resetTime && nowTime <= startTime ) {
            resp.code = 1; resp.desc = 'wrong time to fight'; break;
        }

    }while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    req.args.join = user.world_boss.join;
    req.args.today_id = user.world_boss.today_id;
    requestWorld(req, resp, function() {
        if( resp.code == 0 && resp.data.time ) {
            user.world_boss.time = resp.data.time;
            player.markDirty('world_boss.time');
            player.checkEvent('DoWorldBossAttack');
        }

        onHandled();
    });
}

exports.inspire = function(player, req, resp, onHandled ) {
    do {
        var type = req.args.type;
        if( type != 'cash' && type != 'honor' ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var user = player.user;
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
        
        // 是否超过上限
        var inspired = user.world_boss.inspire * gConfGlobal.WorldBossInspireAttack;
        if( inspired >= gConfGlobal.WorldBossInspireLimit ) {
            resp.code=1; resp.desc='up to inspire limit'; break;
        }

        var cost = 0;
        if( type == 'cash' ) {
            cost = gConfGlobal.WorldBossInspireCash;
        } else if ('honor' == type) {
            cost = gConfGlobal.WorldBossInspireHonor;
        } else {
            resp.code=1; resp.desc='invalid type'; break;
        }

        if (!player.costResource(type, cost)) {
            resp.code = 1; resp.desc = 'not enough resource'; break;
        }

        user.world_boss.inspire += 1;
        player.markDirty('world_boss.inspire');
        
        resp.data[type] = -cost;
    }while(false);

    onHandled();
}

exports.rebirth = function(player, req, resp, onHandled) {
    do {
        var user = player.user;
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
        
        var cashCost = gConfGlobal.WorldBossEndCoolCash;
        if (!player.costResource('cash', cashCost)) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }

        user.world_boss.time = 0;
        player.markDirty('world_boss.time');

        resp.data.cash = -cashCost;

    }while(false);

    onHandled();
}

exports.boss_blood = function(player, req, resp, onHandled) {
    do{
        var user = player.user;
        if( user.status.level < gConfGlobal.WorldBossOpenLevel ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        var nowTime = common.getTime();
        var serverStartDate = new Date(gConfGlobalServer.ServerStartTime*1000);
        var serverStartTime = new Date(serverStartDate.getFullYear(), serverStartDate.getMonth(), serverStartDate.getDate(),0,0,0)/1000;
        if( nowTime < (serverStartTime + (gConfGlobal.WorldBossOpenDays-1)*86400 - 10) ) {
            resp.code = 1; resp.desc = 'not open world boss'; break;
        }
    }while(false);

    if( resp.code == 1 ) {
        onHandled();
        return;
    }

    req.args.join = user.world_boss.join;
    req.args.today_id = user.world_boss.today_id;
    requestWorld(req, resp, onHandled);
}

function getThisWeek() {
    var maxWeek = 1;
    for( var week in gConfWorldBossReward ) {
        if( maxWeek < week ) {
            maxWeek = week;
        }
    }
    
    var today = new Date();
    var thisWeek = Math.ceil((Math.ceil((today/1000 - gConfGlobalServer.ServerStartTime )/86400) - gConfGlobal.WorldBossOpenDays)/7);
    if( thisWeek <= 0 ) {
        thisWeek = 1;
    }

    if( thisWeek > maxWeek ) {
        thisWeek = maxWeek;
    }
    
    return thisWeek;
}

